#include "VBoxLayoutItems.h"

VBoxLayoutItems::VBoxLayoutItems(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::VBoxLayoutItemsClass())
{
    ui->setupUi(this);
    ui->textEdit->setAcceptDrops(false);
    //this->setMouseTracking(true);
    this->setAcceptDrops(true);

    for (int i = 0; i < 10; ++i) {
        ContentItem* item = new ContentItem();
        item->setBoxLayout(this);
        item->setText(QString(i));
        ui->layout->addWidget(item);
    }
}

VBoxLayoutItems::~VBoxLayoutItems()
{
    delete ui;
}

void VBoxLayoutItems::mousePressEvent(QMouseEvent* event)
{
    QWidget::mousePressEvent(event);
}

void VBoxLayoutItems::mouseMoveEvent(QMouseEvent* event)
{
    //if (event->buttons() & Qt::LeftButton) {
    //    qDebug() << "main move";
    //    int distance = (m_startPos - event->pos()).manhattanLength();
    //    if (distance >= QApplication::startDragDistance()) {
    //        QDrag* drag = new QDrag(this);
    //        QMimeData* data = new QMimeData;
    //        drag->setMimeData(data);
    //        drag->exec();
    //        /*if (drag->exec(Qt::MoveAction) == Qt::MoveAction) {
    //        }*/
    //    }
    //}
    QWidget::mouseMoveEvent(event);
}

void VBoxLayoutItems::dragEnterEvent(QDragEnterEvent* event)
{
    //qDebug() << "main dragenter";
    //event->accept();
   /* event->acceptProposedAction();
    if (m_curItem != m_tarItem) {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }*/
    QWidget::dragEnterEvent(event);
}

void VBoxLayoutItems::dragLeaveEvent(QDragLeaveEvent* event)
{
    //qDebug() << "drag leave";
    //event->accept();
    QWidget::dragLeaveEvent(event);
}

//void VBoxLayoutItems::dragMoveEvent(QDragMoveEvent* event)
//{
//    qDebug() << "main dragmove";
//    event->acceptProposedAction();
//
//    /*event->accept();
//    if (m_curItem != m_tarItem) {
//        event->setDropAction(Qt::MoveAction);
//        event->accept();
//    }*/
//    QWidget::dragMoveEvent(event);
//}

void VBoxLayoutItems::dropEvent(QDropEvent* event)
{
    qDebug() << "main dropevent";
    event->acceptProposedAction();
    /*if (m_curItem != m_tarItem && m_tarItem!=nullptr) {
        int idx = ui->layout->indexOf(m_tarItem);
        ContentItem* item = new ContentItem(this);
        item->setBoxLayout(this);
        ui->layout->insertWidget(idx + 1, item);
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }*/
    QWidget::dropEvent(event);
}

void VBoxLayoutItems::setCurrentItem(ContentItem* item)
{
    m_curItem = item;
}

void VBoxLayoutItems::setTargetItem(ContentItem* item)
{
    m_tarItem = item;
}

void VBoxLayoutItems::removeItem(ContentItem* item)
{
    ui->layout->removeWidget(item);
}

void VBoxLayoutItems::addItem(ContentItem* item)
{
    ui->layout->addWidget(item);
}

void VBoxLayoutItems::insertItem(int idx, ContentItem* item)
{
    ui->layout->insertWidget(idx, item);
}

int VBoxLayoutItems::indexOfWidget(ContentItem* item)
{
    return ui->layout->indexOf(item);
}

void VBoxLayoutItems::performDrag()
{
    if (m_curItem) {
        QMimeData* mimeData = new QMimeData;
        mimeData->setProperty("text", m_curItem->getText().toUtf8());
        mimeData->setProperty("finish", m_curItem->isFinished());
        QDrag* drag = new QDrag(this);
        drag->setMimeData(mimeData);
        if (drag->exec(Qt::MoveAction) == Qt::MoveAction) {
            delete m_curItem;
        }
   }
}

//bool VBoxLayoutItems::readFile(const QString& filename)
//{
//    return true;
//}
